﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UtilReportOMS
{
    public partial class FormTrinhKy : Form
    {
        List<clsThongTinTrinhKy> l_tt_tk;
        clsThongTinTrinhKy _clsThongTinTrinhky;
        public bool Exists = false;

        public FormTrinhKy(List<clsThongTinTrinhKy> _l,bool e)
        {           
            InitializeComponent();
            l_tt_tk = _l;
            Exists = e;
            this.Load += new EventHandler(FormTrinhKy_Load);
        }

 #region MyMethod

        public void GetFont()
        {
            foreach (FontFamily f in FontFamily.Families)
            {
                cboFontName.Items.Add(f.Name);
            }
            cboFontStyle.Items.AddRange(new object[] { getStringStyle(FontStyle.Bold), getStringStyle(FontStyle.Italic),
                getStringStyle(FontStyle.Regular), getStringStyle(FontStyle.Strikeout), getStringStyle(FontStyle.Underline) });
            for (int i = 6; i < 70; i += 2)
            {
                cboSize.Items.Add(i.ToString());
            }
        }

        public string getStringStyle(FontStyle s)
        {
            string mv_KIEU_CHU = "";
            switch (s)
            {
                case FontStyle.Bold:
                    mv_KIEU_CHU = "Chữ đậm";
                    break;
                case FontStyle.Italic:
                    mv_KIEU_CHU = "Chữ nghiêng";
                    break;
                case FontStyle.Regular:
                    mv_KIEU_CHU = "Bình thường";
                    break;
                case FontStyle.Strikeout:
                    mv_KIEU_CHU = "Gạch giữa";
                    break;
                case FontStyle.Underline:
                    mv_KIEU_CHU = "Gạch chân";
                    break;
                default:
                    mv_KIEU_CHU = "Bình thường";
                    break;
            }
            return mv_KIEU_CHU;
        }

        //Lấy thông tin trình ký
        private bool GetThongTinTrinhKy(clsThongTinTrinhKy _clsThongTinTrinhky)
        {            
            txtBaoCao.Text = _clsThongTinTrinhky.mv_TEN_BIEUBC;
            txtDai.Text = _clsThongTinTrinhky.mv_CHIEU_DAI.ToString();
            txtRong.Text = _clsThongTinTrinhky.mv_CHIEU_RONG.ToString();
            txtViTriX.Text = _clsThongTinTrinhky.mv_TOADO_NGANG.ToString();
            txtViTriY.Text = _clsThongTinTrinhky.mv_TOADO_DOC.ToString();
            txtNoiDungKy.Text = _clsThongTinTrinhky.mv_NOI_DUNG;
            cboFontName.Text = _clsThongTinTrinhky.mv_FONT_CHU;
            cboSize.Text = _clsThongTinTrinhky.mv_CO_CHU.ToString();
            cboFontStyle.Text = _clsThongTinTrinhky.mv_KIEU_CHU;
            return true;
        }

        public void GetInfoOnForm(clsThongTinTrinhKy _clsThongTinTrinhky)
        {
            _clsThongTinTrinhky.mv_TEN_BIEUBC = txtBaoCao.Text.Trim();
            _clsThongTinTrinhky.mv_CO_CHU = int.Parse(cboSize.SelectedItem.ToString());
            _clsThongTinTrinhky.mv_KIEU_CHU = cboFontStyle.SelectedItem.ToString();
            _clsThongTinTrinhky.mv_NOI_DUNG = txtNoiDungKy.Text;
            _clsThongTinTrinhky.mv_FONT_CHU = cboFontName.SelectedItem.ToString();
            _clsThongTinTrinhky.mv_TOADO_DOC = Int32.Parse(txtViTriY.Text);
            _clsThongTinTrinhky.mv_TOADO_NGANG = Int32.Parse(txtViTriX.Text);
            _clsThongTinTrinhky.mv_CHIEU_DAI = Int32.Parse(txtDai.Text);
            _clsThongTinTrinhky.mv_CHIEU_RONG = Int32.Parse(txtRong.Text);
        }

        public void MakeFontPreview()
        {
            if (cboSize.Text == "" || cboFontStyle.Text == "" || cboFontStyle.Text=="")
            {
                return;
            }
            Font ft;
            try
            {
                switch (cboFontStyle.Text.Trim().ToUpper())
                {
                    case "CHỮ ĐẬM":
                        ft = new Font(cboFontName.Text, float.Parse(cboSize.Text), FontStyle.Bold);
                        break;
                    case "CHỮ NGHIÊNG":
                        ft = new Font(cboFontName.Text, float.Parse(cboSize.Text), FontStyle.Italic);
                        break;
                    case "BÌNH THƯỜNG":
                        ft = new Font(cboFontName.Text, float.Parse(cboSize.Text), FontStyle.Regular);
                        break;
                    case "Gạch giữa":
                        ft = new Font(cboFontName.Text, float.Parse(cboSize.Text), FontStyle.Strikeout);
                        break;
                    case "Gạch chân":
                        ft = new Font(cboFontName.Text, float.Parse(cboSize.Text), FontStyle.Underline);
                        break;
                    default:
                        ft = new Font(cboFontName.Text, float.Parse(cboSize.Text), FontStyle.Regular);
                        break;
                }
                txtNoiDungKy.Font = ft;
            }
            catch (Exception ex)
            {
            }

        }

 #endregion MyMethod


        public clsThongTinTrinhKy ClsThongTinTrinhky
        {
            get { return _clsThongTinTrinhky; }
            set { _clsThongTinTrinhky = value; }
        }

 #region MyEvent

        

        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                clsThongTinTrinhKy cls = new clsThongTinTrinhKy();
                    if (this.chkGhiLai.Checked == true)
                    {
                        if (!UCTK.isConnectSv)
                        {
                            MessageBox.Show("Không connect được server nên mọi thông tin không lưu được","thongbao",MessageBoxButtons.OK,MessageBoxIcon.Information);
                            return;
                        }
                        //da ton tai trong db
                        if (Exists)
                        {
                            cls.updateTrinhKyOnDb(l_tt_tk);
                        }
                        else
                        {
                            cls.insertTrinhKyOnDb(l_tt_tk);
                        }
                    }
                    this.Dispose();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // đổi index
        private void lst_STT_KY_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {                
                int i = lst_STT_KY.SelectedIndex;
                if (i >= 0)
                {
                    _clsThongTinTrinhky = l_tt_tk[i];
                    GetThongTinTrinhKy(_clsThongTinTrinhky);
                    MakeFontPreview();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void cboFontName_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                setInfor();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtNoiDungKy_Leave(object sender, EventArgs e)
        {
            try
            {
                setInfor();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdQuit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void setInfor(){            
                MakeFontPreview();
                GetInfoOnForm(_clsThongTinTrinhky);          
        }

 #endregion

    void FormTrinhKy_Load(object sender, EventArgs e)
    {
        GetFont();
        try
        {
            lst_STT_KY.Items.Clear();
            foreach (clsThongTinTrinhKy c in l_tt_tk)
            {
                lst_STT_KY.Items.Add(c.mv_TEN_DOITUONG);
            }
            lst_STT_KY.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    }
}
